<?php
namespace api\modules\sms\services;
use api\helpers\SmsHelper;
/**
 * User: ziwen
 * Date: 2016/1/28 12:32
 * File: smsapiservice.php
 * Desc: "smsapi 服务层"
 */
//发送短信api服务
class SmsApiService {
    public static $instance = null;
    private $apiName = 'nexmo';
    private $smsApi;
    private $rate = array();
    //需要进行类映射
    private static $classMap = array(
        'nexmo'=>'api\modules\sms\impcommon\nexmo',
        'splio'=>'api\modules\sms\impcommon\splio',
    );

    public function __construct(array $rate){
        $this->rate = $rate;
        $this->getSmsApi();
    }

    //根据名字获取发送短信的api
    private function getSmsApi(){
        $apiname = self::getCodSmsApiName();
        $this->apiName = $apiname;
        $apiname = self::$classMap[$apiname];
        $this->smsApi = $apiname::getInstance();
    }

    //发送短信
    public function sendSms($data){
        return $this->smsApi->sendMsg($data);
    }

    //获取发送短信的api,根据分配的比率
    private function getCodSmsApiName(){
        $apiRates = $this->rate;
        if(!$apiRates){
            $apiName = 'splio';
        }else{
            $randNum = rand(1,100);
            foreach($apiRates as $api=>$rate){
                list($start,$end) = $rate;
                if($randNum>=$start && $randNum<=$end){
                    $apiName = $api;
                    break;
                }
            }
        }
        if(!$apiName){
            $apiName = 'splio';
        }
        return $apiName;
    }


    /**
     * @param $language
     * @param $order_id
     * @param $contact
     * @param $smsCodCode
     * @return bool|array
     * @desc 获取发给不同api的数据
     */
    public function getSmsApiData($language,$order_id,$contact,$smsCodCode){
        $apiName = strtolower(trim($this->apiName));
        if(method_exists($this,$apiName)){
            return  $this->$apiName($language,$order_id,$contact,$smsCodCode);
        }
        return false;
    }

    /**
     * @param $language
     * @param $smsCodCode
     * @return string
     * @desc 获取自定义发送信息的模板
     */
    private static function getNexmoTemp($language,$smsCodCode){
        $ar= "رمز التحقق الخاص بك للتأكد من اتمام طلب الCOD على Jollychic {$smsCodCode}";
        $en= "Your verification code to confirm the COD order at Jollychic: {$smsCodCode}";
        if($language=='ar'){
            return $ar;
        }else{
            return $en;
        }
    }

    /**
     * @param $language
     * @param $order_id
     * @param $contact
     * @param $smsCodCode
     * @return array
     * @desc splioapi发送短信需要的数据
     */
    private function splio($language,$order_id,$contact,$smsCodCode){
        //收集发送短信的数据
        $data = array(
            'message'=> SmsHelper::getTemplateKeyByTcodeAndLanguage('sms_cod_order',$language),
            //收短信的电话
            'contact'=>$contact,
            'cxx'=>array(
                SmsHelper::getPositionByName('smsCodCode')=>$smsCodCode,
            ),
            'verify_code' => $smsCodCode,
            'source_id'=>$order_id,
        );
        return $data;
    }

    /**
     * @param $language
     * @param $order_id
     * @param $contact
     * @param $smsCodCode
     * @return array
     * @desc nexmoapi发送短信需要的数据
     */
    private function nexmo($language,$order_id,$contact,$smsCodCode){
        $data = array(
            'to'=>$contact,
            'from'=> 'jollychic',
            'text'=>self::getNexmoTemp($language,$smsCodCode),
            'verify_code'=>$smsCodCode,
            'source_id'=>$order_id,
            'type'=>'unicode'
        );
        return $data;
    }

}

